package com.hopu.oa.controller.file;

import com.hopu.oa.dao.file.FilePathDao;

import com.hopu.oa.entity.file.FileList;
import com.hopu.oa.entity.file.FilePath;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.file.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Secret
 * @create 2021-07-26 下午 06:50
 */
@Controller
public class FileAjaxController {
    @Autowired
    private FilePathDao filePathDao;
    @Autowired
    private FileService fileService;

    @RequestMapping({"filetypeload", "findfileandpath"})
    public String filetypeupload(@RequestParam(value = "type", defaultValue = "") String type,
                                 Model model,
                                 HttpSession session,
                                 @RequestParam(value = "findfileandpath", defaultValue = "") String findfileandpath) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        String contenttype;
        List<FileList> fileLists = null;
        List<FilePath> filePaths = null;
        System.out.println("type = " + type);
        switch (type) {
            case "":
//                fileLists = filePathMapper.finddocument(user.getUserId(),findfileandpath);
                fileLists = filePathDao.findByUserIdAndFileIsTrash(user.getUserId(), findfileandpath);
                filePaths = filePathDao.findByUserIdAndPathIstrash(user.getUserId(), findfileandpath);
                model.addAttribute("files", fileLists);
                model.addAttribute("paths", filePaths);
                model.addAttribute("isload", 1);
                break;


            case "document":
                fileLists = filePathDao.finddocument(user.getUserId(), findfileandpath);
                System.out.println(fileLists);
                model.addAttribute("files", fileLists);
                model.addAttribute("isload", 1);
                break;
            case "picture":
                contenttype = "image";
                fileLists = filePathDao.findfilecontent(user.getUserId(), contenttype, findfileandpath);
                System.out.println(fileLists);
                model.addAttribute("files", fileLists);
                model.addAttribute("isload", 1);
                break;
            case "music":
                contenttype = "audio";
                fileLists = filePathDao.findfilecontent(user.getUserId(), contenttype, findfileandpath);
                System.out.println(fileLists);
                System.out.println(fileLists);
                model.addAttribute("files", fileLists);
                model.addAttribute("isload", 1);
                break;
            case "video":
                contenttype = "video";
                fileLists = filePathDao.findfilecontent(user.getUserId(), contenttype, findfileandpath);
                System.out.println(fileLists);
                model.addAttribute("files", fileLists);
                model.addAttribute("isload", 1);
                break;
            case "yasuo":
                contenttype = "application";
                fileLists = filePathDao.findfilecontent(user.getUserId(), contenttype, findfileandpath);
                System.out.println(fileLists);
                model.addAttribute("files", fileLists);
                model.addAttribute("isload", 1);
                break;
            case "trash":
                fileLists = filePathDao.findByFileUserIdAndPathIstrash(user.getUserId(), findfileandpath);
                filePaths = filePathDao.findByPathUserIdAndPathIstrash(user.getUserId(), findfileandpath);
                model.addAttribute("paths", filePaths);
                model.addAttribute("files", fileLists);
                model.addAttribute("istrash", 1);
                model.addAttribute("isload", 1);
                break;

            case "share":
                fileLists = filePathDao.findByFileIsshareAndFileIstrash(findfileandpath);
                model.addAttribute("files", fileLists);
                model.addAttribute("isshare", 1);
                model.addAttribute("isload", 1);
                model.addAttribute("userid", user.getUserId());
                break;
            default:
                break;
        }
        model.addAttribute("type", type);
        return "file/filetypeload";
    }


    /**
     * 回收站load 复原
     *
     * @param type
     * @param checkpathids
     * @param checkfileids
     * @param model
     * @return
     */
    @RequestMapping("filereturnback")
    public String filereturnback(
            @RequestParam("type") String type,
            @RequestParam(value = "checkpathids[]", required = false) List<Integer> checkpathids,
            @RequestParam(value = "checkfileids[]", required = false) List<Integer> checkfileids,
            Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        Integer userid = user.getUserId();

        if (checkfileids != null) {
            fileService.filereturnback(checkfileids, userid);
        }
        if (checkpathids != null) {
            fileService.pathreturnback(checkpathids, userid);
        }

        model.addAttribute("type", type);
        return "forward:/filetypeload";

    }


    /**
     * load删除controller
     *
     * @param type
     * @param checkpathids
     * @param checkfileids
     * @param model
     * @return
     */
    @RequestMapping("fileloaddeletefile")
    public String fileloaddeletefile(@RequestParam("type") String type,
                                     @RequestParam(value = "checkpathids[]", required = false) List<Integer> checkpathids,
                                     @RequestParam(value = "checkfileids[]", required = false) List<Integer> checkfileids,
                                     Model model) {

        System.out.println(type + checkpathids + checkfileids);
        if (checkfileids != null) {
            // 删除文件
            fileService.deleteFile(checkfileids);
        }
        if (checkpathids != null) {
            // 删除文件夹
            fileService.deletePath(checkpathids);
        }

        model.addAttribute("type", type);
        return "forward:/filetypeload";
    }


}
